package com.fuershiye.woman.monitor.utils;

import com.fuershiye.woman.monitor.beans.IdAreaId;
import com.fuershiye.woman.monitor.beans.ViewDepartIndicatorReport;
import com.fuershiye.woman.monitor.beans.ViewDepartIndicatorValue;
import com.fuershiye.woman.monitor.beans.ViewReportAllValue;
import com.fuershiye.woman.monitor.constants.ReportType;
import com.fuershiye.woman.monitor.constants.ShowLevel;
import com.fuershiye.woman.monitor.controller.ReportPreAlarmController;
import com.fuershiye.woman.monitor.model.Indicator;
import com.google.common.collect.Maps;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author admin
 */
public class ReportUtil {

	private static final Logger logger = LoggerFactory.getLogger(ReportPreAlarmController.class);

	public static String getEditor(String editor){
		if (!StringUtils.isEmpty(editor)) {
			return editor;
		}else{
			return SecurityUtils.getSubject().getPrincipal().toString();
		}
	}

	public static Integer getNMShowLevel(Integer type, Indicator indicator, Double value, Double lastYearValue) {
		Integer result;

		if (type.equals(ReportType.EMPTY.getId())) {
			result = ShowLevel.SCORE_0.getCode();
		} else if (type.equals(ReportType.NO_DATA.getId())
				|| type.equals(ReportType.NO_CITY.getId())
				|| type.equals(ReportType.NO_COUNTRY.getId())) {
			result = ShowLevel.SCORE_5.getCode();
		} else if (indicator == null || indicator.getRule() == null || value == null) {
			result = null;
		}
		return RuleNeiMengUtil.getShowLevel(indicator.getRule(), value, lastYearValue, indicator.getAim());
	}

	public static Integer getSDShowLevel(Integer type, Integer rule, Double aim, Double base, Double value, Double lastYearValue, Double last2YearValue) {

		if (type.equals(ReportType.EMPTY.getId())) {
			return ShowLevel.SCORE_0.getCode();
		} else if (type.equals(ReportType.NO_DATA.getId())
				|| type.equals(ReportType.NO_CITY.getId())
				|| type.equals(ReportType.NO_COUNTRY.getId())) {
			return ShowLevel.SCORE_5.getCode();
		} else if (rule == null || value == null) {
			return null;
		}
		return RuleShanDongUtil.getShowLevel(rule, value, lastYearValue, last2YearValue, aim, base);
	}


	public static ViewReportAllValue vdi2Vrv(ViewDepartIndicatorValue vdi) {
		ViewReportAllValue vrv = new ViewReportAllValue();
		vrv.setId(vdi.getId());
		vrv.setDepartid(vdi.getDepartid());
		vrv.setIndicator(vdi.getIndicator());
		vrv.setForms(vdi.getForms());
		vrv.setYear(vdi.getYear());

		vrv.setV20(vdi.getV20());
		vrv.setV21(vdi.getV21());
		vrv.setV22(vdi.getV22());
		vrv.setV23(vdi.getV23());
		vrv.setV24(vdi.getV24());
		vrv.setV25(vdi.getV25());

		vrv.setS20(vdi.getS20());
		vrv.setS21(vdi.getS21());
		vrv.setS22(vdi.getS22());
		vrv.setS23(vdi.getS23());
		vrv.setS24(vdi.getS24());
		vrv.setS25(vdi.getS25());

		vrv.setR20(vdi.getR20());
		vrv.setR21(vdi.getR21());
		vrv.setR22(vdi.getR22());
		vrv.setR23(vdi.getR23());
		vrv.setR24(vdi.getR24());
		vrv.setR25(vdi.getR25());

		vrv.setT20(vdi.getT20());
		vrv.setT21(vdi.getT21());
		vrv.setT22(vdi.getT22());
		vrv.setT23(vdi.getT23());
		vrv.setT24(vdi.getT24());
		vrv.setT25(vdi.getT25());

		vrv.setA20(vdi.getA20());
		vrv.setA21(vdi.getA21());
		vrv.setA22(vdi.getA22());
		vrv.setA23(vdi.getA23());
		vrv.setA24(vdi.getA24());
		vrv.setA25(vdi.getA25());

		return vrv;
	}

	public static ViewDepartIndicatorValue mergeReport2One(ViewDepartIndicatorValue vdi, ViewDepartIndicatorReport vdr, int year) {

		switch(year) {
			case 2020:
				vdi.setA20(vdr.getAvalue());
				vdi.setV20(vdr.getRvalue());
				vdi.setS20(vdr.getRshow());
				vdi.setR20(vdr.getRemark());
				vdi.setT20(vdr.getRtype());
				break;
			case 2021:
				vdi.setA21(vdr.getAvalue());
				vdi.setV21(vdr.getRvalue());
				vdi.setS21(vdr.getRshow());
				vdi.setR21(vdr.getRemark());
				vdi.setT21(vdr.getRtype());
				break;
			case 2022:
				vdi.setA22(vdr.getAvalue());
				vdi.setV22(vdr.getRvalue());
				vdi.setS22(vdr.getRshow());
				vdi.setR22(vdr.getRemark());
				vdi.setT22(vdr.getRtype());
				break;
			case 2023:
				vdi.setA23(vdr.getAvalue());
				vdi.setV23(vdr.getRvalue());
				vdi.setS23(vdr.getRshow());
				vdi.setR23(vdr.getRemark());
				vdi.setT23(vdr.getRtype());
				break;
			case 2024:
				vdi.setA24(vdr.getAvalue());
				vdi.setV24(vdr.getRvalue());
				vdi.setS24(vdr.getRshow());
				vdi.setR24(vdr.getRemark());
				vdi.setT24(vdr.getRtype());
				break;
			case 2025:
				vdi.setA25(vdr.getAvalue());
				vdi.setV25(vdr.getRvalue());
				vdi.setS25(vdr.getRshow());
				vdi.setR25(vdr.getRemark());
				vdi.setT25(vdr.getRtype());
				break;
			default:
		}
		return vdi;
	}


	public static ViewReportAllValue mergeReport2One(ViewReportAllValue vdi, ViewDepartIndicatorReport vdr, int year) {

		switch(year) {
			case 2020:
				vdi.setA20(vdr.getAvalue());
				vdi.setV20(vdr.getRvalue());
				vdi.setS20(vdr.getRshow());
				vdi.setR20(vdr.getRemark());
				vdi.setT20(vdr.getRtype());
				break;
			case 2021:
				vdi.setA21(vdr.getAvalue());
				vdi.setV21(vdr.getRvalue());
				vdi.setS21(vdr.getRshow());
				vdi.setR21(vdr.getRemark());
				vdi.setT21(vdr.getRtype());
				break;
			case 2022:
				vdi.setA22(vdr.getAvalue());
				vdi.setV22(vdr.getRvalue());
				vdi.setS22(vdr.getRshow());
				vdi.setR22(vdr.getRemark());
				vdi.setT22(vdr.getRtype());
				break;
			case 2023:
				vdi.setA23(vdr.getAvalue());
				vdi.setV23(vdr.getRvalue());
				vdi.setS23(vdr.getRshow());
				vdi.setR23(vdr.getRemark());
				vdi.setT23(vdr.getRtype());
				break;
			case 2024:
				vdi.setA24(vdr.getAvalue());
				vdi.setV24(vdr.getRvalue());
				vdi.setS24(vdr.getRshow());
				vdi.setR24(vdr.getRemark());
				vdi.setT24(vdr.getRtype());
				break;
			case 2025:
				vdi.setA25(vdr.getAvalue());
				vdi.setV25(vdr.getRvalue());
				vdi.setS25(vdr.getRshow());
				vdi.setR25(vdr.getRemark());
				vdi.setT25(vdr.getRtype());
				break;
			default:
		}
		return vdi;
	}

//	/** 父指标 [/除以] 子指标 */
//	public static ViewReportAllValue setReportValue(ViewReportAllValue vdi, Double parentValue, int year) {
//
//		switch(year) {
//			case 2020:
//				if(Math.notNull(vdi.getV20())){ vdi.setV20(Math.getValue(parentValue/vdi.getV20())); }
//				break;
//			case 2021:
//				if(Math.notNull(vdi.getV21())){ vdi.setV21(Math.getValue(parentValue/vdi.getV21())); }
//				break;
//			case 2022:
//				if(Math.notNull(vdi.getV22())){ vdi.setV22(Math.getValue(parentValue/vdi.getV22())); }
//				break;
//			case 2023:
//				if(Math.notNull(vdi.getV23())){ vdi.setV23(Math.getValue(parentValue/vdi.getV23())); }
//				break;
//			case 2024:
//				if(Math.notNull(vdi.getV24())){ vdi.setV24(Math.getValue(parentValue/vdi.getV24())); }
//				break;
//			case 2025:
//				if(Math.notNull(vdi.getV25())){ vdi.setV25(Math.getValue(parentValue/vdi.getV25())); }
//				break;
//			default:
//		}
//		return vdi;
//	}

	/**
	 * com.fuershiye.woman.monitor.model.DepartIndicator
	 * map.put("2010", "rtype_avalue_rvalue_rshow_remark");
	 * 数据上报的类型_每年的目标值_数值_显示数据展示时效果的规则_每年的备注
	 *
	 * @param map
	 * @param year
	 * @param viewReportAllValue
	 * @return
	 */
	public static Map<String, Object> getYearReport(Map<String, Object> map, int year, ViewReportAllValue viewReportAllValue) {

		switch(year) {
			case 2020:
				map.put("2020", getStrValue(viewReportAllValue.getT20())+"_"+getStrValue(viewReportAllValue.getA20())+"_"+getStrValue(viewReportAllValue.getV20())+"_"+getStrValue(viewReportAllValue.getS20())+"_"+getStrValue(viewReportAllValue.getR20())+"");
				break;
			case 2021:
				map.put("2021", getStrValue(viewReportAllValue.getT21())+"_"+getStrValue(viewReportAllValue.getA21())+"_"+getStrValue(viewReportAllValue.getV21())+"_"+getStrValue(viewReportAllValue.getS21())+"_"+getStrValue(viewReportAllValue.getR21())+"");
				break;
			case 2022:
				map.put("2022", getStrValue(viewReportAllValue.getT22())+"_"+getStrValue(viewReportAllValue.getA22())+"_"+getStrValue(viewReportAllValue.getV22())+"_"+getStrValue(viewReportAllValue.getS22())+"_"+getStrValue(viewReportAllValue.getR22())+"");
				break;
			case 2023:
				map.put("2023", getStrValue(viewReportAllValue.getT23())+"_"+getStrValue(viewReportAllValue.getA23())+"_"+getStrValue(viewReportAllValue.getV23())+"_"+getStrValue(viewReportAllValue.getS23())+"_"+getStrValue(viewReportAllValue.getR23())+"");
				break;
			case 2024:
				map.put("2024", getStrValue(viewReportAllValue.getT24())+"_"+getStrValue(viewReportAllValue.getA24())+"_"+getStrValue(viewReportAllValue.getV24())+"_"+getStrValue(viewReportAllValue.getS24())+"_"+getStrValue(viewReportAllValue.getR24())+"");
				break;
			case 2025:
				map.put("2025", getStrValue(viewReportAllValue.getT25())+"_"+getStrValue(viewReportAllValue.getA25())+"_"+getStrValue(viewReportAllValue.getV25())+"_"+getStrValue(viewReportAllValue.getS25())+"_"+getStrValue(viewReportAllValue.getR25())+"");
				break;
			default:
		}
		return map;
	}

    public static String getStrValue(String value) {
	    String result = value==null?"":value.toString();
		return result;
    }

    public static String getStrValue(Integer value) {
	    String result = value==null?"":value.toString();
		return result;
    }

    public static String getStrValue(Double value) {
	    String result = value==null?"":value.toString();
		result = result.equals("0.0")? "0":result;
		if(result.endsWith(".0")){
			result = result.substring(0, result.lastIndexOf(".0"));
		}
		return result.equals("")? "": new BigDecimal(result).toString();
    }

	public static Map<String, Object> countColor(Long areaid,  List<IdAreaId> departmentList, List<ViewReportAllValue> reportList, Integer year) {

		int reach_more = 0;
		int reach = 0;
		int un = 0;
		int un_more = 0;
		int un_bed = 0;

		for(ViewReportAllValue viewReportAllValue:reportList){
			if(BaseUtil.departBelongArea(viewReportAllValue.getDepartid(), areaid, departmentList)){

				int color = ReportUtil.getShowColorByYear(viewReportAllValue, year);

				switch(color) {
					case 10:
						un_bed++;
						break;
					case 20:
						un_more++;
						break;
					case 30:
						un++;
						break;
					case 40:
						reach++;
						break;
					case 50:
						reach_more++;
						break;
					default:
				}
			}
		}

		Map<String, Object> map = Maps.newHashMap();
		map.put("REACH_MORE", reach_more);
		map.put("REACH", reach);
		map.put("UN", un);
		map.put("UN_MORE", un_more);
		map.put("UN_BED", un_bed);

		return map;
	}

	public static Integer getShowColorByYear(ViewReportAllValue viewReportAllValue, Integer year){
		if(year==null){
			return null;
		}

		switch(year) {
			case 2020:
				return viewReportAllValue.getS20();
			case 2021:
				return viewReportAllValue.getS21();
			case 2022:
				return viewReportAllValue.getS22();
			case 2023:
				return viewReportAllValue.getS23();
			case 2024:
				return viewReportAllValue.getS24();
			case 2025:
				return viewReportAllValue.getS25();
			default:
		}

		return null;
	}


	public static Double getValueByYear(ViewReportAllValue viewReportAllValue, Integer year){
		if(year==null){
			return null;
		}

		switch(year) {
			case 2020:
				return viewReportAllValue.getV20();
			case 2021:
				return viewReportAllValue.getV21();
			case 2022:
				return viewReportAllValue.getV22();
			case 2023:
				return viewReportAllValue.getV23();
			case 2024:
				return viewReportAllValue.getV24();
			case 2025:
				return viewReportAllValue.getV25();
			default:
		}

		return null;
	}

	public static void count() {
		System.out.println(11/3+1);
		System.out.println(11%3);
	}

	public static void main(String[] args) throws Exception {

//		System.out.println(getStrValue(null));
		System.out.println(getStrValue(new Double(0)));
		System.out.println(getStrValue(new Double(0.0)));
		System.out.println(getStrValue(new Double(0.00005)));
		System.out.println(getStrValue(new Double(0.1)));
		System.out.println(getStrValue(new Double(1.)));
		System.out.println(getStrValue(new Double(1.0)));
		System.out.println(getStrValue(new Double(1.00)));
		System.out.println(getStrValue(new Double(1.01)));
		System.out.println(getStrValue(new Double(1.010)));
		System.out.println(getStrValue(new Double(1.1)));
		System.out.println(getStrValue(new Double(1.10)));
		System.out.println(getStrValue(new Double(120.0)));
		System.out.println(getStrValue(new Double(1234567891)));
		System.out.println(getStrValue(new Double(321654.123456789)));
		System.out.println(getStrValue(new Double(3.18628951E7)));
		System.out.println(getStrValue(new Double(31862895.1)));

//		count();
//		test();
//		System.out.println(getRate(130d, 71d));
	}

}